package com.gyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gyh.entity.Member;
import com.gyh.entity.Msg;
import com.gyh.mapper.CourseMapper;
import com.gyh.service.MemberService;
import com.gyh.service.MsgService;
import com.gyh.util.JWTUtil;
import com.gyh.util.Result;
import com.gyh.vo.CourseVo;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private MsgService msgService;
    @Resource
    private MemberService memberService;
    @Resource
    private CourseMapper courseMapper;

    @RabbitListener(queues = "user.22.queue")
    public void setMsg(String msg){
        System.out.println("接收、存储消息"+msg);

    }
    @GetMapping("/getReserveMsg")
    public Result getReserveMsg(HttpServletRequest request) throws InterruptedException {
        String token = request.getHeader("token");
        String phone = null;
        try {
            Claims claims = JWTUtil.parseJWT(token);
            phone = claims.getSubject();
        } catch (Exception e) {
            return Result.error(401,"登录失效");
        }
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Member member = memberService.getOne(wrapper);
        QueryWrapper<Msg> msgQueryWrapper = new QueryWrapper<>();
        msgQueryWrapper.eq("member_id",member.getId());
        List<Msg> msgs = msgService.list(msgQueryWrapper);
        List<CourseVo> courseVos = courseMapper.selectCourseAndStatus(member.getId());
        return Result.success(null,courseVos);
    }
    @GetMapping("/changeStatus")
    public Result changeStatus(Integer msgId){
        UpdateWrapper<Msg> msgUpdateWrapper = new UpdateWrapper<>();
        msgUpdateWrapper.set("status",2).eq("id",msgId);
        boolean update = msgService.update(msgUpdateWrapper);
        if (!update){
            return Result.error(402,"更新异常");
        }
        return Result.success("更新成功",null);
    }
    @DeleteMapping("{msgId}")
    public Result delete(@PathVariable("msgId") Integer msgId){
        boolean b = msgService.removeById(msgId);
        if (!b){
            return Result.error(402,"删除失败");
        }
        return Result.success("删除成功",null);
    }
}
